import { h } from 'vue';
import { DialogPlugin, MessagePlugin } from 'tdesign-vue-next';
import { useUserStore } from '@/store';
import loginComponent from './login.vue';
import $eventBus from '@/utils/eventBus';


//打开登录窗口
export function openLogin() {
  const vnode = h(loginComponent);
  const instance = DialogPlugin({
    header: '微信扫码 关注登录',
    body: vnode as any,
    footer: null,
    width: 280,
    destroyOnClose: false, //关闭时销毁子元素
  });

  const userStore = useUserStore();
  $eventBus.on('receiveMsgEvent', (data : string) => {
    console.log('receive', data);
    var ret = JSON.parse(data);
    if (ret.Type === 'login.success') {
      $eventBus.emit('LoginSuccessEvent');
      MessagePlugin.success('登录成功');
      let token = JSON.parse(ret.Message).access_token;
      userStore.saveToken(token);
      instance.hide();
    }
  });
}